#!/usr/bin/env python
# -*- coding: utf-8 -*-

import threading

from controls import mc
from utils import log
from controls import data_sight_base_mysql


def set_sight_base_item_to_mc(pfx, key, item):
    return mc.set_item(pfx, key, item)


def get_sight_base_all(p_cityid):
    try:
        sight_base_item = mc.get_item('sight', p_cityid)
        if sight_base_item is None:
            item = data_sight_base_mysql.get_sight_base_all(p_cityid)
            t = threading.Thread(target=set_sight_base_item_to_mc, args=('sight', p_cityid, item))
            t.start()
            return item
        else:
            return sight_base_item
    except Exception, e:
        log.exp(e)
        return False


def add_sight_base_item(p_cityid, item):
    res_mc = mc.set_item('sight', p_cityid, item)
    if not res_mc:
        return False
    for one in range(0, len(item)):
        res_db = data_sight_base_mysql.add_sight_base_item(p_cityid, item[one])
        if not res_db:
            mc.set_item('sight', p_cityid, None)
            return False


if __name__ == '__main__':
    p_cityid = 1
    # item = [ {'sightid': 1L, 'name': u'1', 'cname': u'\u8C61\u9F3B\u5C71'}, {'sightid': 2L, 'name': u'2', 'cname': u'\u9756\u6c5f\u738b\u5e9c'}, {'sightid': 4L, 'name': u'4', 'cname': u'\u4e03\u661f\u516c\u56ed'}, {'sightid': 5L, 'name': u'5', 'cname': u'\u65e5\u6708\u53cc\u5854'}, {'sightid': 6L, 'name': u'6', 'cname': u'\u6f13\u6c5f'}, {'sightid': 7L, 'name': u'7', 'cname': u'\u53e0\u5f69\u5c71'}, {'sightid': 8L, 'name': u'8', 'cname': u'\u4f0f\u6ce2\u5c71'}, {'sightid': 9L, 'name': u'9', 'cname': u'\u8001\u4eba\u5c71'}, {'sightid': 10L, 'name': u'10', 'cname': u'\u4e24\u6c5f\u56db\u6e56'}, {'sightid': 11L, 'name': u'11', 'cname': u'\u6728\u9f99\u6e56'}, {'sightid': 12L, 'name': u'12', 'cname': u'\u5218\u4e09\u59d0\u5927\u89c2\u56ed'}, {'sightid': 13L, 'name': u'13', 'cname': u'\u82a6\u7b1b\u5ca9'}, {'sightid': 14L, 'name': u'14', 'cname': u'\u5357\u6eaa\u5c71\u516c\u56ed'}, {'sightid': 15L, 'name': u'15', 'cname': u'\u6b63\u9633\u6b65\u884c\u8857'}, {'sightid': 16L, 'name': u'16', 'cname': u'\u5c27\u5c71'}, {'sightid': 17L, 'name': u'17', 'cname': u'\u7a7f\u5c71\u516c\u56ed'}, {'sightid': 18L, 'name': u'18', 'cname': u'\u6842\u6797\u690d\u7269\u56ed'}, {'sightid': 19L, 'name': u'19', 'cname': u'\u897f\u5c71\u516c\u56ed'}]
    # add_sight_base_item( p_cityid, item )
    print get_sight_base_all(p_cityid)
